package com.btex.app.treaty.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.btex.app.R;
import com.btex.app.tools.Tools;
import com.btex.app.treaty.GangGanAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/10/15
 * @time 下午6:05
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */

public class LeverSetDialog extends Dialog {

    //    @BindView(R.id.treary_wheelview)
//    WheelView mTrearyWheelview;
//    @BindView(R.id.dialog_lever_tv_diss)
//    TextView mDialogLeverTvDiss;
//    @BindView(R.id.dialog_lever_tv_sure)
//    TextView mDialogLeverTvSure;

    @BindView(R.id.lever_list_dialog_quxiao)
    TextView lever_list_dialog_quxiao;

    getValueLIstener mGetValueLIstener;
    @BindView(R.id.lever_dialog_ganggan)
    RecyclerView mLeverDialogGanggan;
    private Context mContext;
    private ArrayList<String> mStrings;
    private String mAllow_cross;
    private ArrayList<String> mStrings1;

    public LeverSetDialog(@NonNull Context context, ArrayList<String> strings, String Allow_cross) {
        super(context, R.style.coupon_style);
        mContext = context;
        mStrings = strings;
        mAllow_cross = Allow_cross;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lever_set_dialog);
        ButterKnife.bind(this);
        mStrings1 = new ArrayList<>();

        Log.i("wpeofpwefpwojfe", mAllow_cross);
        if (mAllow_cross.equals("-1")) {

        } else {
//            mStrings1.add(mContext.getString(R.string.heyue_quancang));
        }


        for (int i = 0; i < mStrings.size(); i++) {
            mStrings1.add(mStrings.get(i));

        }

        initRecy();

        int viewHeight = Tools.getViewHeight(mLeverDialogGanggan);
        int viewHeight1 = Tools.getViewHeight(lever_list_dialog_quxiao);

        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 1);

        if (viewHeight == 0 || viewHeight1 == 0) {
            if (mAllow_cross.equals("-1")) {
                attributes.height = (int) (heightPixels * 0.66);
            } else {
                attributes.height = (int) (heightPixels * 0.7);
            }
        } else {
            if (mAllow_cross.equals("-1")) {
                attributes.height = (int) (viewHeight + viewHeight1);
            } else {
                attributes.height = (int) (viewHeight + viewHeight1);
            }
        }


        //让Dialog位置居中
        attributes.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);


        initListener();


    }

    private void initListener() {
        lever_list_dialog_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initRecy() {

        GangGanAdapter gangGanAdapter = new GangGanAdapter();
        mLeverDialogGanggan.setLayoutManager(new LinearLayoutManager(mContext));
        mLeverDialogGanggan.setAdapter(gangGanAdapter);
        gangGanAdapter.setNewData(mStrings1);
        gangGanAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                String s = (String) adapter.getData().get(position);
                if (mGetValueLIstener != null) {
                    mGetValueLIstener.get(s);
                    dismiss();
                }
            }
        });
    }

//
//    @OnClick({R.id.dialog_lever_tv_diss, R.id.dialog_lever_tv_sure})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.dialog_lever_tv_diss:
//                dismiss();
//                break;
//            case R.id.dialog_lever_tv_sure:
////
////                if (mGetValueLIstener != null) {
////                    mGetValueLIstener.get(mTrearyWheelview.getSelectedText());
////                }
//
//                dismiss();
//
//                break;
//        }
//    }


    public interface getValueLIstener {

        public void get(String data);
    }


    public void getValue(getValueLIstener getValueLIstener) {

        this.mGetValueLIstener = getValueLIstener;

    }
}

